// ElderService.java
package com.community.service;

import com.community.dao.ElderDao;
import com.community.dao.impl.ElderDaoImpl;
import com.community.model.Elder;
import java.util.List;

public class ElderService {
    private final ElderDao elderDao = new ElderDaoImpl();

    public void addElder(Elder elder) {
        validateElder(elder);
        elderDao.insert(elder);
    }

    public void updateElder(Elder elder) {
        if (elder.getId() == null) throw new IllegalArgumentException("老人ID不能为空");
        validateElder(elder);
        elderDao.update(elder);
    }

    public void deleteElder(int id) {
        elderDao.delete(id);
    }

    public Elder getElderById(int id) {
        return elderDao.getById(id);
    }

    public List<Elder> getAllElders() {
        return elderDao.getAll();
    }

    public List<Elder> searchEldersByName(String name) {
        if (name == null || name.trim().isEmpty()) return getAllElders();
        return elderDao.searchByName(name.trim());
    }

    private void validateElder(Elder elder) {
        if (elder.getName() == null || elder.getName().trim().isEmpty()) {
            throw new IllegalArgumentException("姓名不能为空");
        }
        if (elder.getAge() < 60 || elder.getAge() > 120) {
            throw new IllegalArgumentException("年龄应在60-120岁之间");
        }
        if (elder.getContact() == null || elder.getContact().trim().isEmpty()) {
            throw new IllegalArgumentException("联系方式不能为空");
        }
    }
}